package com.bootdo.common.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bootdo.common.dao.DictDao;
import com.bootdo.common.service.DictService;
import com.bootdo.system.domain.UserDO;
import com.smart.common.utils.R;
import com.smart.common.utils.StringUtils;
import com.smart.domain.DictDO;


@Service
public class DictServiceImpl implements DictService {
    @Autowired
    private DictDao dictDao;

    @Override
    public DictDO get(Long id) {
        return dictDao.get(id);
    }

    @Override
    public List<DictDO> list(Map<String, Object> map) {
        return dictDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return dictDao.count(map);
    }

    @Override
    public int save(DictDO dict) {
        return dictDao.save(dict);
    }

    @Override
    public int update(DictDO dict) {
        return dictDao.update(dict);
    }

    @Override
    public int remove(Long id) {
        return dictDao.remove(id);
    }

    @Override
    public int batchRemove(Long[] ids) {
        return dictDao.batchRemove(ids);
    }

    @Override

    public List<DictDO> listType() {
        return dictDao.listType();
    }

    @Override
    public String getName(String type, String value) {
        Map<String, Object> param = new HashMap<String, Object>(16);
        param.put("type", type);
        param.put("value", value);
        String rString = dictDao.list(param).get(0).getName();
        return rString;
    }

    @Override
    public List<DictDO> getHobbyList(UserDO userDO) {
        Map<String, Object> param = new HashMap<>(16);
        param.put("type", "hobby");
        List<DictDO> hobbyList = dictDao.list(param);

        if (StringUtils.isNotEmpty(userDO.getHobby())) {
            String userHobbys[] = userDO.getHobby().split(";");
            for (String userHobby : userHobbys) {
                for (DictDO hobby : hobbyList) {
                    if (!Objects.equals(userHobby, hobby.getId().toString())) {
                        continue;
                    }
                    hobby.setRemarks("true");
                    break;
                }
            }
        }

        return hobbyList;
    }

    @Override
    public List<DictDO> getSexList() {
        Map<String, Object> param = new HashMap<>(16);
        param.put("type", "sex");
        return dictDao.list(param);
    }

    @Override
    public List<DictDO> listByType(String type) {
        Map<String, Object> param = new HashMap<>(16);
        param.put("type", type);
        return dictDao.list(param);
    }

	@Override
	public Map<String, List<DictDO>> listDicts(String[] keys) {
		List<DictDO> list = dictDao.listDicts(keys);
		Map<String, List<DictDO>> map = new HashMap<String,List<DictDO>>();
		for(String s:keys) {
			map.put(s, null);//当查询不到数据时,返回一个空值
		}
		for(DictDO dictDo : list) {
			String type = dictDo.getType();
			List<DictDO> dictDoList = map.get(type);
			if(dictDoList == null) {
				dictDoList = new ArrayList<DictDO>();
				map.put(type, dictDoList);
			}
			dictDoList.add(dictDo);
		}
		return map;
	}

	public R checkDuplicate(DictDO dictDO, String s){
        //验证value，type是否唯一
        Map<String,Object> map = new HashMap<>();
        map.put("value", dictDO.getValue());
        map.put("type",dictDO.getType());
        if ("1".equals(s)){
            List<DictDO> list = dictDao.list(map);
            if (list.size() > 0){
                return R.error("当前类型的数据值已存在");
            }
        }
        if ("2".equals(s)){
            map.put("id",dictDO.getId());
            if (dictDao.checkUpdateDup(map) > 0){
                return R.error("当前类型的数据值已存在");
            }
        }
        return R.ok();
    }
}
